package com.dhms.vostok1.data.spot_patrol

import com.dhms.uikit.select_bottom_sheet.SelectItem
import com.squareup.moshi.Json
import io.realm.kotlin.ext.realmListOf
import io.realm.kotlin.ext.toRealmList
import io.realm.kotlin.types.RealmList
import io.realm.kotlin.types.RealmObject
import io.realm.kotlin.types.annotations.PrimaryKey
import java.util.*

open class TaskData : RealmObject {
    @PrimaryKey
    var _id: String = UUID.randomUUID().toString()

    /* 任务类型 */
    @Json(name = "task_type")
    var taskType: PlanType = PlanType.UNKNOWN

    /*  设备id */
    @Json(name = "device_id")
    var deviceId: Long? = null

    /*  设备name */
    @Json(name = "device_name")
    var deviceName: String? = null

    /*  开始时间 */
    @Json(name = "start_time")
    var startTime: String? = null

    /* 超时时间 */
    @Json(name = "overtime_str")
    var overtimeStr: String? = null


    /*  完成时间 */
    @Json(name = "complete_time")
    var completeTime: String? = null

    /* 点检项 */
    @Json(name = "patrol_items")
    var patrolItems: PatrolItemsData? = null

    /* 任务状态 */
    private var code: Int = TaskStatus.UNKNOWN.code

    @Json(name = "status")
    var taskStatus: TaskStatus?
        get() {
            return try {
                TaskStatus.fromInt(code)
            } catch (e: IllegalArgumentException) {
                TaskStatus.UNKNOWN
            }
        }
        set(value) {
            code = value?.code ?: TaskStatus.UNKNOWN.code
        }

    /*  执行人列表 */
    @Json(name = "executors")
    var executors: RealmList<SelectItem>? = realmListOf()

    /* 任务id */
    @Json(name = "task_id")
    var taskId: Long? = null

    /*任务组id*/
    @Json(name = "group_id")
    var groupID: String? = null

    /* 异常项的数量 */
    @Json(name = "abnormal_patrol_count")
    var abnormalPatrolCount: Int? = null

    /* 创建时间 */
    @Json(name = "created_at")
    var createdAt: String? = null

    /* 更新时间 */
    @Json(name = "updated_at")
    var updatedAt: String? = null

    /* 是否完成 */
    @Json(name = "is_complete")
    var isComplete: Boolean? = null

    /* 是否异常 */
    @Json(name = "is_abnormal")
    var isAbnormal: Boolean? = null

    /* 设备部件信息 */
    @Json(name = "device_tree_chart_config")
    var deTreeChartConfig: String? = null

    var error: String? = null


    /**这个是用到了 realm 数据库 task data moshi parsing error
    *  - Property 'TaskData.bleBeacons' has been removed.
    * */
    @Json(name = "ble_beacons")
    var bleBeaconsJson: List<String>?
        get() {
            return bleBeacons.toList()
        }
        set(value) {
            bleBeacons = value?.toRealmList() ?: realmListOf()
        }

    var bleBeacons: RealmList<String> = realmListOf()


}

class PatrolItemsData : RealmObject {
    /*  五感检查 */
    @Json(name = "five_senses")
    var fiveSenses: RealmList<FiveSensesData> = realmListOf()

    /*  抄表检查 */
    @Json(name = "meter_reading")
    var meterReading: RealmList<MeterReadingData> = realmListOf()

    /*  点检仪检查 */
    @Json(name = "spot_detector")
    var spotDetector: RealmList<SpotDetectorData> = realmListOf()
}
